package com.beyondsoft.mcms.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class UmsApiServiceImpl  {

	private static final String UMS_CHARSET = "GB2312";
	private static final String UMS_URL = "http://gd.ums86.com:8899/sms/Api/"; 
	private static final String UMS_SPCODE = "201347";
	//private static final String UMS_URL = "http://gd.ums86.com"; 
	//private static final String UMS_SPCODE = "075500700054";
	private static final String UMS_LOGINNAME = "admin";
	private static final String UMS_PASSWORD = "jxh321321";
	private static final String UMS_API_SEND = "Send.do"; 
	
	public static void main(String[] args) throws Exception{
		System.out.println(sendShortMsgByPost("aa会员，您好：请于2013年9月1日14:00出席ss会议，谢谢！","13418757459"));
	}
	
	
    public static Map<String, String> sendShortMsgByPost(String msgContent,String phoneNumber) 
    		throws UnsupportedEncodingException, ClientProtocolException, IOException{
    	List<NameValuePair> params=new ArrayList<NameValuePair>();
    	
    	
    	Map<String, String> returnMsg = new HashMap<String, String>();
    	
		HttpClient httpclient = new DefaultHttpClient();
		
		try {
			HttpPost httppost = new HttpPost(new StringBuffer(UMS_URL).append(UMS_API_SEND).toString().trim());
	        params.add(new BasicNameValuePair("SpCode", UMS_SPCODE));
	        params.add(new BasicNameValuePair("LoginName", UMS_LOGINNAME));
	        params.add(new BasicNameValuePair("Password", UMS_PASSWORD));
	        params.add(new BasicNameValuePair("MessageContent", msgContent));
	        params.add(new BasicNameValuePair("UserNumber", phoneNumber));
	        params.add(new BasicNameValuePair("SerialNumber", ""));
	        params.add(new BasicNameValuePair("ScheduleTime", ""));
	        params.add(new BasicNameValuePair("f", "1"));
	        
			httppost.setEntity(new UrlEncodedFormEntity(params, UMS_CHARSET));
			
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity resEntity = response.getEntity();
			
			returnMsg.put("statusCode", String.valueOf(response.getStatusLine().getStatusCode()));
			
			if (response.getStatusLine().getStatusCode() == 200
					&& resEntity != null) {
				String returnStr = EntityUtils.toString(resEntity, UMS_CHARSET);
				if (null != returnStr && !"".equals(returnStr.trim())
						&& returnStr.trim().indexOf("&") > 0) {
					String[] retrunArray = returnStr.split("&");
					for (int i = 0; i < retrunArray.length; i++) {
						if(retrunArray[i].trim().indexOf("=") <= 0) continue;
						String[] retrunArray0 = retrunArray[i].split("=");
						if (retrunArray0.length>1){
							returnMsg.put(retrunArray0[0], retrunArray0[1]);
						}
					}
					EntityUtils.consume(resEntity);
				}
			}
			return returnMsg;
			
		} finally {
			try {
				httpclient.getConnectionManager().shutdown();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
    }
    

}
